package com.kstech.exccheck.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.kstech.exccheck.R;


/**
 * 
 * @author wanghaibin
 * @created 2016-9-30 ����3:37:18
 * @since v1.0
 */
public class DateTimePickDialogUtil implements OnDateChangedListener {
	private DatePicker datePicker;
	private String dateTime;
	private String initDateTime;
	private Activity activity;

	/**
	 * 初始日期时间值，作为弹出窗口的标题和日期时间初始值
	 */
	public DateTimePickDialogUtil(Activity activity, String initDateTime) {
		this.activity = activity;
		this.initDateTime = initDateTime;
	}

	/**
	 * 弹出日期时间选择框方法
	 *
	 * @param inputDate
	 *            :为需要设置的日期时间文本编辑框
	 * @return
	 */
	public void dateTimePicKDialog(final TextView inputDate) {

		// 初始化
		LayoutInflater layoutInflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View layout = layoutInflater.inflate(
				R.layout.date_picker, null);
		final PopupWindow changeStatusPopUp = new PopupWindow(activity);
		changeStatusPopUp.setContentView(layout);
		changeStatusPopUp.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
		changeStatusPopUp.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
		// 去掉黑色的边框，很闹心啊
		changeStatusPopUp.setBackgroundDrawable(null);
		changeStatusPopUp.setFocusable(true);

		datePicker = (DatePicker) layout.findViewById(R.id.datepicker);

		Calendar calendar = Calendar.getInstance();
		if (!(null == initDateTime || "".equals(initDateTime))) {
			calendar = this.getCalendarByInintData(initDateTime);
		}

		datePicker.init(calendar.get(Calendar.YEAR),
				calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH), this);

		changeStatusPopUp
				.showAtLocation(layout, Gravity.CENTER, 0, 0);

		layout.findViewById(R.id.sureCheckFinishBtn).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if ("".equals(dateTime)) {
					dateTime = new SimpleDateFormat("yyyy-MM-dd")
							.format(Calendar.getInstance().getTime());
				}
				inputDate.setText(dateTime);
				changeStatusPopUp.dismiss();
			}
		});

	}

	/**
	 * @param initDateTime
	 *            初始日期时间值 字符串型
	 * @return Calendar
	 */
	private Calendar getCalendarByInintData(String initDateTime) {
		Calendar calendar = Calendar.getInstance();

		int[] yearMonthDay = DateUtil.getYearMonthDay(initDateTime);

		calendar.set(yearMonthDay[0], yearMonthDay[1], yearMonthDay[2]);
		return calendar;
	}

	@Override
	public void onDateChanged(DatePicker arg0, int arg1, int arg2, int arg3) {
		// 获得日历实例
		Calendar calendar = Calendar.getInstance();

		calendar.set(datePicker.getYear(), datePicker.getMonth(),
				datePicker.getDayOfMonth());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		dateTime = sdf.format(calendar.getTime());
	}

}
